package com.example.uniapp

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import io.dcloud.feature.sdk.DCUniMPSDK
import io.dcloud.feature.sdk.Interface.IUniMP
import org.json.JSONObject
import java.util.*

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

    /** unimp小程序实例缓存 */
    var uniMPCaches = HashMap<String, IUniMP>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        view.findViewById<Button>(R.id.button_first).setOnClickListener {
//            findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
            try {
                val uniMP = DCUniMPSDK.getInstance().openUniMP(context,"__UNI__73A72AD")
                uniMPCaches[uniMP.appid] = uniMP
            } catch (e: Exception) {

            }
        }


        //回传数据给小程序
        DCUniMPSDK.getInstance().setOnUniMPEventCallBack { appid, event, data, callback ->
            Log.i("cs", "onUniMPEventReceive    event=$event")
            val json = JSONObject()
            val data = JSONObject()

            data.put("code","123456789")

            json.put("jsonrpc","2.0")
            json.put("result",data)
            json.put("id","1234567")
            //回传数据给小程序
            callback.invoke(json)
        }


        // 监听关闭
        DCUniMPSDK.getInstance().setUniMPOnCloseCallBack {
            //小程序被关闭需要对实例缓存删除操作
            if (uniMPCaches.containsKey(it)) {
                uniMPCaches.remove(it)
            }
        }

        DCUniMPSDK.getInstance().setDefMenuButtonClickCallBack { appid, id ->
            when (id) {
                "gy" -> {
                    Log.e("unimp", "点击了关于$appid")
                    //宿主主动触发事件
                    val data = JSONObject()
                    try {
                        val uniMP: IUniMP? = uniMPCaches[appid]
                        if (uniMP != null) {
                            data.put("sj", "点击了关于")
                            uniMP.sendUniMPEvent("gy", data)
                        }
                    } catch (e: java.lang.Exception) {
                        e.printStackTrace()
                    }
                }
                "hqdqym" -> {
                    val uniMP: IUniMP? = uniMPCaches[appid]
                    if (uniMP != null) {
                        Log.e("unimp", "当前页面url=" + uniMP.currentPageUrl)
                    } else {
                        Log.e("unimp", "未找到相关小程序实例")
                    }
                }
            }
        }
    }




    override fun onDestroy() {
        super.onDestroy()
        uniMPCaches.clear()
    }
}